package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.InsHttpCmdLog;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/24 1:54
 * @description
 */
@Repository("insHttpCmdLogArchiverDao")
public class insHttpCmdLogArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_HTTP_CMD_LOG WHERE TASK_ID = ?");
        List<InsHttpCmdLog> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(InsHttpCmdLog.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `HTTP_URL`, `AUTH_TYPE`, `HTTP_TYPE`, `AUTH_PARAM1`, `AUTH_PARAM2`, `RESULT`, `RESULT_DESC`, `TASK_ID`, `TEMPLATE_ID`, `CREATE_DATE`, `RESULT_LENGTH`, `PARTITION_CODE`");
        sql.append(" FROM INS_HTTP_CMD_LOG WHERE TASK_ID in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<InsHttpCmdLog> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsHttpCmdLog insHttpCmdLog = new InsHttpCmdLog();
                insHttpCmdLog.setId(resultSet.getLong("ID"));
                insHttpCmdLog.setUrl(resultSet.getString("URL"));
                insHttpCmdLog.setAuthType(resultSet.getString("AUTH_TYPE"));
                insHttpCmdLog.setHttpType(resultSet.getString("HTTP_TYPE"));
                insHttpCmdLog.setAuthParam1(resultSet.getString("AUTH_PARAM1"));
                insHttpCmdLog.setAuthParam2(resultSet.getString("AUTH_PARAM2"));
                insHttpCmdLog.setResult(resultSet.getInt("RESULT"));
                insHttpCmdLog.setResultDesc(resultSet.getString("RESULT_DESC"));
                insHttpCmdLog.setResultLength(resultSet.getInt("RESULT_LENGTH"));
                insHttpCmdLog.setTaskId(resultSet.getLong("TASK_ID"));
                insHttpCmdLog.setTemplateId(resultSet.getLong("TEMPLATE_ID"));
                insHttpCmdLog.setCreateDate(resultSet.getTimestamp("CREATE_DATE"));
                insHttpCmdLog.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insHttpCmdLog;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_http_cmd_log`");
        sql.append(" (`ID`, `HTTP_URL`, `AUTH_TYPE`, `HTTP_TYPE`, `AUTH_PARAM1`, `AUTH_PARAM2`, `RESULT`, `RESULT_DESC`, `TASK_ID`, `TEMPLATE_ID`, `CREATE_DATE`, `RESULT_LENGTH`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id,:url,:authType,:httpType,:authParam1,:authParam2,:result,:resultDesc,:taskId,:templateId,:createDate,:resultLength,:partitionCode,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {

    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_HTTP_CMD_LOG WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
